package com.ruoyi.report_tg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_tg.domain.bo.TgsybgBaDetailBo;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.report_tg.mapper.TgsybgBaDetailMapper;
import com.ruoyi.report_tg.service.ITgsybgBaDetailService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 土报告详细Service业务层处理
 *
 * @author zyb
 * @date 2024-05-24
 */
@RequiredArgsConstructor
@Service
@Transactional
public class TgsybgBaDetailServiceImpl implements ITgsybgBaDetailService {

    private final TgsybgBaDetailMapper baseMapper;

    /**
     * 查询土报告详细
     */
    @Override
    public TgsybgBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询土报告详细列表
     */
    @Override
    public TableDataInfo<TgsybgBaDetailVo> queryPageList(TgsybgBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TgsybgBaDetail> lqw = buildQueryWrapper(bo);
        Page<TgsybgBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询土报告详细列表
     */
    @Override
    public List<TgsybgBaDetailVo> queryList(TgsybgBaDetailBo bo) {
        LambdaQueryWrapper<TgsybgBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TgsybgBaDetail> buildQueryWrapper(TgsybgBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TgsybgBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, TgsybgBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(ObjUtil.isNotNull(bo.getQualId()), TgsybgBaDetail::getQualId, bo.getQualId());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbolValue()), TgsybgBaDetail::getSymbolValue, bo.getSymbolValue());
        lqw.eq(bo.getNum1() != null, TgsybgBaDetail::getNum1, bo.getNum1());
        lqw.eq(bo.getNum2() != null, TgsybgBaDetail::getNum2, bo.getNum2());
        lqw.eq(bo.getJiancjg() != null, TgsybgBaDetail::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), TgsybgBaDetail::getJiegpd, bo.getJiegpd());
        return lqw;
    }

    /**
     * 新增土报告详细
     */
    @Override
    public Boolean insertByBo(TgsybgBaDetailBo bo) {
        TgsybgBaDetail add = BeanUtil.toBean(bo, TgsybgBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改土报告详细
     */
    @Override
    public Boolean updateByBo(TgsybgBaDetailBo bo) {
        TgsybgBaDetail update = BeanUtil.toBean(bo, TgsybgBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TgsybgBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除土报告详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
